import {Module} from "vuex";
import {isEmpty, isInteger, isUndefined} from "@/utils/helpers";
import router from "@/router";

const app: Module<any, any> = {
    namespaced: true,
    state: {
        siteName: '管理平台',
        loading: false,
        tabs: [],
    },
    getters: {},
    mutations: {
        changeLoading: (state, val) => {
            state.loading = val
        },
        pushTab: (state, val) => {
            if (isEmpty(val)) {
                return;
            }
            for (let route of state.tabs) {
                if (route.fullPath === val.fullPath) {
                    return;
                }
            }
            if (!val.fullPath) {
                if (val.redirect) {
                    val.fullPath = val.redirect
                } else {
                    val.fullPath = val.path
                }
            }
            state.tabs.push(val)
        },
        spliceTabs(state, {start, count, retainIndex}) {
            let retainItem = []
            if (isInteger(retainIndex)) {
                retainItem = state.tabs[retainIndex]
            }
            if (isUndefined(count)) {
                count = state.tabs.length
            }
            state.tabs.splice(start, count)
            if (!isEmpty(retainItem)) {
                router.push(retainItem.fullPath)
            }
            this.commit("app/pushTab", retainItem);
        }
    },
    actions: {},
}

export default app
